package com.opoc.controller;

import com.opoc.config.BaseContext;
import com.opoc.mapper.EnterpriseMapper;
import com.opoc.pojo.Enterprise;
import com.opoc.pojo.Result;
import com.opoc.service.EnterpriseService;
import com.opoc.service.SystemLogService;
import com.opoc.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import java.text.ParseException;
import java.util.List;
import java.util.Map;


/**
 * @program: opoc
 * @description:
 * @author: lyx
 * @create: 2023-10-22 15:11
 */
@RestController
@RequestMapping("/admin/company")
public class EnterpriseController {
    @Autowired
 EnterpriseService enterpriseService;
    @Autowired
  EnterpriseMapper enterpriseMapper;
    @Autowired
    SystemLogService systemLogService;

    /**
     * 获取企业基本信息*/
    @GetMapping("/basic")
    public Result getEnterpriseBasicInfo(
            @RequestParam(value ="company_id",required = false) String company_id,
            @RequestParam(value ="company_name",required = false) String company_name,
            @RequestParam(value ="belong_region",required = false) String belong_region
    ){
        List<Enterprise> data=enterpriseService.getEnterpriseBasicInfo(company_id,company_name,belong_region);
        return data.isEmpty() ? Result.error("无数据") : Result.success(data);
    }
/*
* 上传图片
* */
    @PostMapping("/upload")
    public Result uploadImage(@RequestParam(value = "pool_distribution", required = false) MultipartFile pool_distribution,
                              @RequestParam(value = "security_manage_net", required = false) MultipartFile security_manage_net,
                              @RequestParam("company_id") String company_id) {
      if (!enterpriseMapper.validEnterpriseById(company_id)){
        return Result.error("企业不存在");
      }
      if (pool_distribution == null && security_manage_net == null){
        return Result.error("未选择上传文件");
      }

      Map<String, String> result = enterpriseService.uploadImage(pool_distribution, security_manage_net, company_id);

      return result.isEmpty() ? Result.error("上传失败") : Result.success(result);
    }


    /**
     * 添加企业
     */
    @RequestMapping("/add")
    public Result addEnterprise(Enterprise enterprise) throws ParseException {
        String result=enterpriseService.addEnterprise(enterprise);
        systemLogService.addLog("企业管理", "添加", BaseContext.getCurrentId(), "access".equals(result)? "成功" : "失败");
        return "access".equals(result)?Result.success():Result.error(result);

    }

    /**
     * 删除企业*/
    @DeleteMapping ("/delete")
    public Result deleteEnterprise(@RequestParam("company_ids") List<String> company_ids){
       if(company_ids.isEmpty()){
           return Result.error("is_null");
       }
        String err = enterpriseService.deleteEnterprise(company_ids);
        return err != null? Result.error(err):Result.success();

    }

    /**
    * 批量导入企业
    */
    @PostMapping("/import")
    public Result importEnterprise(@RequestParam("import_file")MultipartFile file) throws Exception {
        String fileExtension= FileUtils.getFileExtension(file);
        if(FileUtils.EMPTY.equals(fileExtension)){
            return Result.error("仅支持xls和xlsx格式文件");
        }else{
          String err=enterpriseService.importEnterprise(file);
          systemLogService.addLog("企业管理", "导入", BaseContext.getCurrentId(), err == null ? "成功" : "失败");
          return err== null ? Result.success():Result.error(err);
        }
    }

    /**
     * 批量导出企业
     */
    @GetMapping("/export")
    public Result exportEnterprise(@RequestParam("company_ids")List<String> ids) throws IOException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException, ParseException {
    if(ids.isEmpty()){
        return Result.error("未选择企业");
    }
       String file_url= enterpriseService.exportEnterprise(ids);
        systemLogService.addLog("企业管理", "导出", BaseContext.getCurrentId(), file_url != null ? "成功" : "失败");
       return file_url==null?Result.error("导出失败"):Result.success(file_url);
    }
}
